using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TaugeLibrary.Classes.Abstracts;
using TaugeLibrary.Classes;
using TaugeLibrary.Interfaces;
using TaugeLibrary.UserControls;
using TaugeFramework.Classes;
using System.Collections;
using System.Reflection;
using TaugeLibrary.Classes.Attributes;
using TaugeLibrary.Enums;

namespace TaugeFramework.Forms
{
    public partial class frmBusinessUnit : Form,IComplete
    {
        ABusinessUnitManager m_unit_mgr;

       

        #region for select
        Database m_db;

        string m_sql;
        string m_curr_order;
        #endregion

        #region for form
        ABusinessUnitUI m_unit_ui;
        ABusinessUnit m_unit;        
        #endregion

        #region for view mode
        eViewMode m_curr_view_mode = eViewMode.QUERY;
        eViewMode m_prev_view_mode = eViewMode.QUERY;

        bool m_insert_enabled = true;
        bool m_update_enabled = true;
        bool m_delete_enabled = true;
        
        bool m_post_enabled = true;
        bool m_unpost_enabled = true;

        bool m_print_enabled = true;
        
        #endregion

        #region for list
        frmList m_form_list = null;
        eViewMode m_initial_view_mode = eViewMode.QUERY;
        #endregion

        public eViewMode InitialViewMode { get { return m_initial_view_mode; } }

        public frmBusinessUnit(frmList p_frmlist, ABusinessUnit p_unit, eViewMode p_viewmode)
        {
            InitializeComponent();

            m_form_list = p_frmlist;
            
            setUnitManager(p_frmlist.UnitManager);            
            
            WriteUI(p_unit);            

            m_initial_view_mode = p_viewmode;
            setViewMode(m_initial_view_mode);

            

        }

        public void WriteUI(ABusinessUnit p_unit, bool save_to_curr_unit)
        {
            if(save_to_curr_unit)
                m_unit = p_unit;
            m_unit_ui.writeUI(p_unit);            
        }

        public void WriteUI(ABusinessUnit p_unit)
        {
            WriteUI(p_unit, true);
        }

        public frmBusinessUnit(ABusinessUnitManager p_unit_mgr)
        {
            InitializeComponent();

            

            setUnitManager(p_unit_mgr);


            LoadLastRecord();
            setViewMode(eViewMode.QUERY);

        }

        public void setUnitManager(ABusinessUnitManager p_unit_mgr)
        {
            m_unit_mgr = p_unit_mgr;

            #region UI
            //title
            this.Text = m_unit_mgr.getUnitName();
            lblTitle.Text = m_unit_mgr.getUnitName();

            //ui
            m_unit_ui = (ABusinessUnitUI)m_unit_mgr.createUnitUI();            
            setPnlMainSize(m_unit_ui.Width, m_unit_ui.Height);
            this.pnlUI.Controls.Clear();
            this.pnlUI.Controls.Add(m_unit_ui);
            m_unit_ui.Dock = DockStyle.Fill;
            #endregion

            m_db= Program.Framework.getDatabase(m_unit_mgr.getDBType());

            

        }

        public void LoadLastRecord()
        {
            #region data
            //show last record or new            
            Program.DebugBox(m_unit_mgr.getLastRecordSql());
            DataTable _dt = m_db.executeDataTable(m_unit_mgr.getLastRecordSql());
            if (_dt.Rows.Count > 0)
            {
                m_unit = m_unit_mgr.GetUnitFromList(_dt.Rows[0]);
            }

            if (m_unit == null)
                m_unit = m_unit_mgr.createUnit();
            WriteUI(m_unit);


            
            #endregion

        }

        public void setViewMode(eViewMode p_view_mode)
        {
            m_prev_view_mode = m_curr_view_mode;
            m_curr_view_mode = p_view_mode;
            m_unit_ui.setViewMode(m_curr_view_mode);                                
            Program.fMain.RefreshToolbar();
        }

        
        public void setPnlMainSize(int p_Width, int p_Height)
        {
            this.Width = p_Width + 8;
            this.Height = p_Height + 122;
        }

        

        



        #region IPrintable Members

        public int doPrint()
        {
            return 0;
        }

        public bool isPrintable()
        {
            return (m_print_enabled && 
                    ((m_curr_view_mode & (eViewMode.QUERY | eViewMode.LIST)) != 0)
                    );
        }

        #endregion

        #region ISaveAble Members

        public int doSave()
        {
            ABusinessUnit x = m_unit_mgr.createUnit();                
            if ((m_curr_view_mode & (eViewMode.INSERT | eViewMode.UPDATE )) != 0)
            {
                m_unit_ui.readUI(x);

                string _action = "";
                if (m_curr_view_mode == eViewMode.INSERT)
                    _action = LibConstants.eINSERT;
                else if(m_curr_view_mode==eViewMode.UPDATE)
                    _action = LibConstants.eUPDATE;
                
                
                if (x.doAction(_action))
                {
                    x.doRead();
                    WriteUI(x);
                    m_unit = x;
                    //setViewMode(eViewMode.QUERY);


                    if (m_form_list != null)
                    {
                        m_form_list.CallBack(true, this);
                        if(m_initial_view_mode==eViewMode.QUERY)
                            setViewMode(m_prev_view_mode);
                    }
                    else
                        setViewMode(m_prev_view_mode);
                    
                    
                }
                else
                {
                    MessageBox.Show(x.ErrorMessage);
                }
                
            }
            else if(m_curr_view_mode == eViewMode.SEARCH)
            {
            }
            x=null;
            return 0;
        }

        public int doCancel()
        {
            if ((m_curr_view_mode & (eViewMode.INSERT | eViewMode.UPDATE | eViewMode.SEARCH)) != 0)
            {                
                WriteUI(m_unit);
                setViewMode(eViewMode.QUERY);
            }
            else
            {
                this.Close();
            }
            return 0;

        }

        public bool isSaveable()
        {
            return ((m_curr_view_mode & (eViewMode.INSERT | eViewMode.UPDATE | eViewMode.SEARCH)) != 0);
                   
        }

        #endregion

        #region IPostable Members

        public int doPosting()
        {
            return 0;
        }

        public int doUnPosting()
        {
            return 0;

        }

        public bool isPostable()
        {
            return (m_post_enabled &&
                        ((m_curr_view_mode & (eViewMode.QUERY | eViewMode.LIST)) != 0)
                        );

        }

        public bool isUnPostable()
        {
            return (m_unpost_enabled &&
                        ((m_curr_view_mode & (eViewMode.QUERY | eViewMode.LIST)) != 0)
                        );
        }

        #endregion

        #region ISearchable Members

        public int doSearch()
        {
            return 0;
        }

        public bool isSearchable()
        {
            return ((m_curr_view_mode & (eViewMode.QUERY | eViewMode.LIST)) != 0);
        }

        #endregion

        #region INavigateable Members

        public int doMoveNext()
        {
            
            return 0;
        }

        public int doMovePrev()
        {
            
            return 0;
        }

        public int doMoveFirst()
        {
            
            return 0;
        }

        public int doMoveLast()
        {
            
            return 0;
        }

        public bool isNavigateable()
        {
            return ((m_curr_view_mode & (eViewMode.QUERY | eViewMode.LIST)) != 0);
        }

        #endregion

        #region IActionable Members

        public int doQuery()
        {
            return 0;
        }

        public int doInsert()
        {
            ABusinessUnit x = m_unit_mgr.createUnit();
            WriteUI(x,false);
            setViewMode(eViewMode.INSERT);
            return 0;
        }

        public int doUpdate()
        {
            setViewMode(eViewMode.UPDATE);         
            return 0;
        }

        public int doDelete()
        {
            if (MessageBox.Show("Apakah yakin mau menghapus data ini?", "Hapus data", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                ABusinessUnit x=null;

                    
                x = m_unit_mgr.createUnit();
                m_unit_ui.readUI(x);
            

                if (x != null)
                {
                    if (x.doAction(LibConstants.eDELETE))
                    {
                        if (m_form_list != null)
                            m_form_list.CallBack(true, this);
                    }
                    else
                    {
                        MessageBox.Show(x.ErrorMessage);
                    }
                }
                setViewMode(m_curr_view_mode);
                //if (m_curr_view_mode == eViewMode.LIST)
                //    RefreshList();
            }
            return 0;
        }

        public bool isQueryable()
        {
            return ((m_curr_view_mode & (eViewMode.LIST)) != 0);
        }

        public bool isInsertable()
        {
            return (m_insert_enabled &&
                        ((m_curr_view_mode & (eViewMode.QUERY | eViewMode.LIST) )!= 0)
                        );

        }

        public bool isUpdateable()
        {
            return (m_update_enabled &&
                        ((m_curr_view_mode & (eViewMode.QUERY | eViewMode.LIST)) != 0)
                        );
        }

        public bool isDeleteable()
        {
            return (m_delete_enabled &&
                    ((m_curr_view_mode & (eViewMode.QUERY | eViewMode.LIST)) != 0)
                    );

        }

        #endregion

        #region IRefreshAble Members

        public int doRefresh()
        {
            //m_update_list = true;
            if (m_curr_view_mode == eViewMode.LIST)
            {
                //RefreshList();
            }
            else
            {
                ABusinessUnit x = m_unit_mgr.createUnit();
                m_unit_ui.readUI(x);
                if (!x.doRead())
                {
                    MessageBox.Show(x.ErrorMessage);
                }
            }
            return 0;
        }

        public bool isRefreshable()
        {
            return ((m_curr_view_mode & (eViewMode.QUERY | eViewMode.LIST)) != 0);
        }

        #endregion

        private void frmBusinessUnit_Shown(object sender, EventArgs e)
        {
            if (m_initial_view_mode == eViewMode.DELETE)
                doDelete();
            
        }

        

        

    }
}